//package com.ip.getip.service.impl;
//
//import com.ip.getip.dao.NameRepository;
//import com.ip.getip.entity.NameEntity;
//import java.io.BufferedReader;
//import java.io.InputStreamReader;
//import java.net.URL;
//import java.net.URLConnection;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//import java.util.stream.Collectors;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
///**
// * @Description TODO
// * @Author leixin
// * @Date 2019/9/28 下午9:03
// * @Version 1.0
// **/
//@Service
//public class Xvideos {
//
//    @Autowired
//    private NameRepository nameRepository;
//
//    public void service() {
//        //执行播放页获取下载链接
//        ExecutorService service = Executors.newFixedThreadPool(5);
//        for (int i = 0; i < 500; i++) {
//            int finalI = i;
//            service.execute(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        videos(finalI);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            });
//        }
//    }
//
//    public void videos(int num) throws Exception {
//        System.setProperty("https.proxyHost", "127.0.0.1");
//        System.setProperty("https.proxyPort", "1080");
//        StringBuilder result = new StringBuilder();
//        BufferedReader in = null;
//        URL realUrl = new URL("https://www.xvideos.com/?k=%E7%BE%8E%E5%A5%B3&p=" + num);
//        // 打开和URL之间的连接
//        URLConnection connection = realUrl.openConnection();
//        // 建立实际的连接
//        connection.connect();
//        // 定义 BufferedReader输入流来读取URL的响应
//        in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
//        String line;
//        while ((line = in.readLine()) != null) {
//            result.append(line);
//        }
//        String name = "<a[^>]*>([^<]*)</a>";
//        Pattern n = Pattern.compile(name);
//        Matcher l = n.matcher(result.toString());
//        List<String> list = new ArrayList<>();
//        while (l.find()) {
//            for (int i = 0; i < l.groupCount(); i++) {
//                list.add(l.group(i).trim());
//            }
//        }
//        List<String> filterList = list.stream().filter(a -> a.contains("title=") && a.contains("/video"))
//            .collect(Collectors.toList());
//        List<String> urlList = new ArrayList<>();
//        String url = "(?<=<a href=\").*?(?=\" title=)";
//        Pattern p = Pattern.compile(url);
//        filterList.forEach(a -> {
//            Matcher m = p.matcher(a);
//            if (m.find()) {
//                urlList.add("https://www.xvideos.com" + m.group(0).trim());
//            }
//        });
//        //执行播放页获取下载链接
////        ExecutorService service = Executors.newFixedThreadPool(10);
////        urlList.forEach(a -> {
////            service.execute(new Runnable() {
////                @Override
////                public void run() {
////                    try {
////                        playMemory(a);
////                    } catch (Exception e) {
////                        e.printStackTrace();
////                    }
////                }
////            });
////        });
//        //执行播放页获取下载链接
//        urlList.forEach(a -> {
//            try {
//                playMemory(a);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        });
//    }
//
//    public void playMemory(String url) throws Exception {
//        System.setProperty("https.proxyHost", "127.0.0.1");
//        System.setProperty("https.proxyPort", "1080");
//        StringBuilder result = new StringBuilder();
//        BufferedReader in = null;
//        URL realUrl = new URL(url);
//        // 打开和URL之间的连接
//        URLConnection connection = realUrl.openConnection();
//        // 建立实际的连接
//        connection.connect();
//        // 定义 BufferedReader输入流来读取URL的响应
//        in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
//        String line;
//        while ((line = in.readLine()) != null) {
//            result.append(line);
//        }
//        String urlName = "";
//        String urlPlay = "";
//        String name = "(?<=html5player.setVideoTitle\\(\').*?(?=\'\\);)";
//        Pattern n = Pattern.compile(name);
//        Matcher l = n.matcher(result.toString());
//        if (l.find()) {
//            urlName = l.group(0).trim();
//        }
//        String playUrl = "(?<=html5player.setVideoUrlHigh\\(\').*?(?=\'\\);)";
//        Pattern r = Pattern.compile(playUrl);
//        Matcher m = r.matcher(result.toString());
//        if (m.find()) {
//            urlPlay = m.group(0).trim();
//        }
//        NameEntity nameEntity = new NameEntity();
//        nameEntity.setName(urlName);
//        nameEntity.setPath(urlPlay);
//        nameRepository.save(nameEntity);
//    }
//}
